
package com.fivebrms.view.independent
{
	import org.osflash.thunderbolt.Logger;
	
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.Text;
	import mx.controls.Image;
	import mx.containers.VBox;
	
	import flash.events.KeyboardEvent;
	
	import mx.events.FlexEvent;
	import mx.binding.utils.BindingUtils;
	import mx.core.UIComponent;
	
	import com.fivebrms.view.independent.About;
	
	import mx.validators.StringValidator;
	
	import flash.display.BitmapData;	
	import flash.display.Bitmap;
	import flash.geom.Matrix;
			
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.fivebrms.events.IndependentEvent;
	import com.fivebrms.model.MainModelLocator;
	import com.fivebrms.vo.IndependentVO;
	
	import flash.net.registerClassAlias;  

	import mx.managers.HistoryManager;

	import org.alivepdf.pdf.PDF;
	import org.alivepdf.display.Display;
	import org.alivepdf.layout.Layout;
	import org.alivepdf.fonts.FontFamily;
	import org.alivepdf.saving.Download;
	import org.alivepdf.saving.Method;
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.layout.Unit;
	import org.alivepdf.layout.Size;
	import org.alivepdf.images.ImageFormat;
	import org.alivepdf.pages.Page;
	import org.alivepdf.drawing.Blend;
		
	public class FormFields extends VBox
	{
		private var model : MainModelLocator = MainModelLocator.getInstance();
		
		public var inp_contractorName : TextInput;
		public var inp_contractorAddress : TextInput;
		
		public var btn_submitIndependentForm : Button;
		public var pdfContainerVbox : VBox;
		
		private var indepVO : IndependentVO;
		
		private var holdOriginalAboutText : Text;
		
		public function FormFields()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
        private function creationCompleteHandler(event:FlexEvent) : void
        {
            Logger.info("independent..1..form formfields....");
           
			
            Logger.info("independent..2..form formfields....");
			
		}

		public function exportPdf( fileName : String ) : void
		{
            Logger.info("start..trying to export pdf....!");
         
            //
            // start pdf
            //
            
            //
        	// grab the model locator
        	//
			pdfContainerVbox = this.model.aboutIndependent;
						
			Logger.info("test pdf......1..");
			
			pdfContainerVbox
			var bd : BitmapData = getBitmapData( UIComponent( pdfContainerVbox ) );
			
			Logger.info("test pdf......1.1.");
			var image : Bitmap = new Bitmap( bd );
			
			Logger.info("test pdf......1.3.");
			image.width = bd.width;
			image.height = bd.height;

			Logger.info("test pdf......2..");
			
			var pdf : PDF = new PDF (Orientation.PORTRAIT, Unit.MM, Size.LETTER);

			Logger.info("test pdf......3..");
			
			pdf.addPage();
			pdf.setTitle("TEST PDF Generator using AlivePDF");
			pdf.setFont(FontFamily.ARIAL,"",24);
			pdf.setAuthor("me");
			pdf.setCreator("meCreator");
			pdf.setLeftMargin(5);
			pdf.setRightMargin(5);
			pdf.setSubject("Subject String");
			pdf.setTopMargin(5);
			pdf.setXY(30,30);
			pdf.writeText(12,"This is a writeText function from the FlexNG tool");
//			pdf.addPage(Orientation.PORTRAIT, Unit.MM,Size.LETTER);
			pdf.addImage(image, 0, 0, 0, 0, ImageFormat.JPG, 100, 1, "FitToPage", "Normal", true, "");
			
			Logger.info("test pdf......4..");
			
			pdf.save( Method.REMOTE, "http://@URL@:8080/forms/createPDF", Download.INLINE, fileName );
            //
            // end pdf
            //
			
			Logger.info("end..trying to export pdf....3..");
		}
		  		
		private function getBitmapData( target : UIComponent ) : BitmapData
		{
			var bd : BitmapData = new BitmapData(target.width, target.height);
			
			Logger.info("get ui size..target.width->" + target.width);
			Logger.info("get ui size..target.height->" + target.height);

			var matrix:Matrix = new Matrix();
			bd.draw(target, matrix);
			
			return bd;
		}
		

	}
}

